// ListBbsState.cpp: implementation of the CListBbsState class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ansiterminal.h"
#include "ListBbsState.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CListBbsState::CListBbsState()
{
	m_oldFocusLine = -1;
}

CListBbsState::~CListBbsState()
{

}


void CListBbsState::Deactivate()
{
	clearOldFocusLine();
}

void CListBbsState::clearOldFocusLine()
{
	if(m_oldFocusLine != -1)
	{
		CSize sizeScr = m_pScreen->GetSize();
		setScreenEffects(CRect(1, m_oldFocusLine, sizeScr.cx - 1, m_oldFocusLine), EffectNone);
		m_oldFocusLine = -1;
	}
}


BOOL CListBbsState::OnClick(CPoint ptCursorCell)
{
	OnMouseMove(ptCursorCell, 0);
	if(m_clickAction == mcaExit)
	{
		m_pKeyboard->OnKeyDown(VK_LEFT, 0);
	}
	else if(m_clickAction == mcaHome)
	{
		m_pKeyboard->OnKeyDown(VK_HOME, 0);
	}
	else if(m_clickAction == mcaEnd)
	{
		m_pKeyboard->OnKeyDown(VK_END, 0);
	}
	else if(m_clickAction == mcaPrev)
	{
		m_pKeyboard->OnKeyDown(VK_PRIOR, 0);
	}
	else if(m_clickAction == mcaNext)
	{
		m_pKeyboard->OnKeyDown(VK_NEXT, 0);
	}
	else if(m_clickAction == mcaEnter)
	{
		char **chars = m_pScreen->GetChars();
		CSize sizeScr = m_pScreen->GetSize();
		int pointedLine = -1;
		for(int y = 3; y <= sizeScr.cy - 2; y++)
		{
			if(chars[y][0] != 32)
			{
				pointedLine = y;
				break;
			}
		}
		ASSERT(pointedLine != -1);
		int y1 = min(pointedLine, ptCursorCell.y);
		int y2 = max(pointedLine, ptCursorCell.y);
		UINT arrowChar = pointedLine > ptCursorCell.y? VK_UP: VK_DOWN;
		for(y = y1; y < y2; y++)
		{
			m_pKeyboard->OnKeyDown(arrowChar, 0);
		}
		m_pKeyboard->OnKeyDown(VK_RIGHT, 0);
	}
	else if(m_clickAction == mcaNone)
	{
		// do nothing
		return FALSE;
	}
	else
	{
		ASSERT(FALSE);
	}
	return TRUE;
}

BOOL CListBbsState::OnMouseMove(CPoint ptCell, UINT nFlags)
{
	HCURSOR hCursor = NULL;
	
	char **chars = m_pScreen->GetChars();
	CSize sizeScr = m_pScreen->GetSize();
	
	int focusLine = -1;
	m_clickAction = mcaNone;
	if(ptCell.y <= 2)
	{
		hCursor = LoadCursor(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDC_HOME));
		m_clickAction = mcaHome;
	}
	else if(ptCell.y >= sizeScr.cy - 1)
	{
		hCursor = LoadCursor(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDC_END));
		m_clickAction = mcaEnd;
	}
	else if(ptCell.x <= 11)
	{
		hCursor = LoadCursor(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDC_EXIT));
		m_clickAction = mcaExit;
	}
	else if(ptCell.x >= 64)
	{
		if(ptCell.y <= 10)
		{
			hCursor = LoadCursor(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDC_PREV));
			m_clickAction = mcaPrev;
		}
		else
		{
			hCursor = LoadCursor(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDC_NEXT));
			m_clickAction = mcaNext;
		}
	}
	else
	{
		if( (ptCell.y >= 3) && (ptCell.y <= sizeScr.cy - 2) && 
			lastNonSpaceCol(chars, ptCell.y, 0, sizeScr.cx - 1) >= 8)
		{
			hCursor = LoadCursor(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDC_HAND1));
			m_clickAction = mcaEnter;
			focusLine = ptCell.y;
		}
	}
	if(focusLine != m_oldFocusLine)
	{
		if(m_oldFocusLine != -1)
			setScreenEffects(CRect(1, m_oldFocusLine, sizeScr.cx - 1, m_oldFocusLine), EffectNone);
		if(focusLine != -1)
			setScreenEffects(CRect(1, focusLine, sizeScr.cx - 1, focusLine), EffectHighlight);
		
		m_oldFocusLine = focusLine;
	}
	m_pScreen->SetCursor(hCursor);
	return BOOL(hCursor);
}
